//
//  NSDictionary+WLSafeDictionary.m
//  BDJustice
//
//  Created by 汪磊 on 2017/6/24.
//  Copyright © 2017年 bigKumasann. All rights reserved.
//

#import "NSDictionary+WLSafeDictionary.h"

@implementation NSDictionary (WLSafeDictionary)

- (id)safeObjectForKey:(NSString *)key {
    if (!key || ![key isKindOfClass:[NSString class]]) {
        return nil;
    }
    
    id value = [self objectForKey:key];
    if (value && ![value isKindOfClass:[NSNull class]]) {
        return value;
    }
    
    return nil;
}

- (NSString *)stringForKey:(NSString *)key {
    id value = [self safeObjectForKey:key];
    
    if (value) {
        if ([value isKindOfClass:[NSString class]]) {
            return value;
        }
        
        if ([value isKindOfClass:[NSNumber class]]) {
            return [value stringValue];
        }
    }
    
    return nil;
    
}

- (NSArray *)arrayForKey:(NSString *)key {
    id value = [self safeObjectForKey:key];
    
    if (value && [value isKindOfClass:[NSArray class]]) {
        return value;
    }
    
    return nil;
}

- (NSDictionary *)dictionaryForKey:(NSString *)key {
    id value = [self safeObjectForKey:key];
    
    if (value && [value isKindOfClass:[NSDictionary class]]) {
        return value;
    }
    
    return value;
}

@end
